<?php
namespace App\Models\Promotion;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Redis;

class PromotionOrderGiftCard extends Model
{

    protected $table = 'promotion_order_gift_card';

    protected $primaryKey = 'id';

    public $timestamps = false;

    public $incrementing = false;

    public static function getCache( $id ) {

        $card = Redis::get('G_WDH_BILL_PROMOTION_CARD_' . $id );

        if ( empty( $card ) ) {

            $card_data = self::where('promotion_id',$id)->get()->toArray();
            if ( empty( $card_data ) ) {
                return null;
            }

            foreach ( $card_data as $item ) {
                $card[] = [
                    'id' => $item['id'],
                    'card_id' => $item['card_id'],
                    'card_name' => $item['card_name'],
                    'card_num' => $item['card_num'],
                    'most' => $item['most'],
                ];
            }

            Redis::setex( 'G_WDH_BILL_PROMOTION_CARD_' . $id , 604800 , json_encode( $card ));

        } else {
            $card = json_decode( $card , true );
        }

        return $card;

    }
    
}
